package com.digital.service.impl;

import com.digital.entity.Class;
import com.digital.mapper.ClassMapper;
import com.digital.service.ClassService;
import com.digital.dto.ClassPageQueryDTO;
import com.digital.result.PageResult;
import com.digital.vo.ClassVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ClassServiceImpl implements ClassService
{
    @Autowired
    private ClassMapper classMapper;
    @Override
    public PageResult getList(ClassPageQueryDTO classPageQueryDTO)
    {
        if(classPageQueryDTO.getPage() == null)
        {
            classPageQueryDTO.setPage(1);
        }
        if(classPageQueryDTO.getPageSize() == null)
        {
            classPageQueryDTO.setPageSize(10);
        }
        PageResult pageResult = new PageResult();
        PageHelper.startPage(classPageQueryDTO.getPage(),classPageQueryDTO.getPageSize());
        List<ClassVO> list =  classMapper.list(classPageQueryDTO);
        PageInfo<ClassVO> pageInfo = new PageInfo<>(list);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setRecords(pageInfo.getList());
        return pageResult;
    }

    @Override
    public void addClass(Class classEntity)
    {
        classMapper.addClass(classEntity);
    }
}
